package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by wolfcode-lanxw
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/list")
    public String list(QueryObject qo, Model model){
        model.addAttribute("pageInfo",roleService.selectList(qo));
        return "role/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        model.addAttribute("allPermissions",permissionService.selectAll());
        if(id!=null){
            model.addAttribute("selfPermissions",permissionService.selectByRoleId(id));
            model.addAttribute("role",roleService.get(id));
        }
        return "role/input";
    }
    @RequiredPermission(name = "角色新增/编辑",expression = "role.saveOrUpdate")
    // 处理保存或修改请求
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids) {
        if (role.getId() == null) { // 保存
            roleService.save(role,ids);
        } else { // 修改
            roleService.update(role,ids);
        }
        return "redirect:/role/list"; // 重定向，浏览器再发一次请求
    }
    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }
}
